/**
 * 引动页面
 */
import { isPro, ossPath } from './utils/index';
import BootScenes from './scenes/boot';
// import LoaderScenes from './scenes/loader';
import MainScenes from './scenes/main';
// import ConfirmScene from './scenes/confirm';
import Wachat from './utils/wechat';

const resize = (game: Phaser.Game) => {
  const canvas = document.querySelector('canvas');
  const windowWidth = window.innerWidth;
  const windowHeight = window.innerHeight;
  const windowRatio = windowWidth / windowHeight;
  const gameRatio = Number(game.config.width) / Number(game.config.height);
  if (!canvas) return;
  if (windowRatio < gameRatio) {
    canvas.style.width = `${windowWidth}px`;
    canvas.style.height = `${windowWidth / gameRatio}px`;
  } else {
    canvas.style.width = `${windowHeight * gameRatio}px`;
    canvas.style.height = `${windowHeight}px`;
  }
};

window.onload = () => {
  Wachat.init();

  const config: Phaser.Types.Core.GameConfig = {
    type: Phaser.AUTO,
    backgroundColor: '#fff',
    // backgroundColor: '#4968af',
    parent: 'game',
    width: 750,
    height: (750 / window.innerWidth) * window.innerHeight,
    scale: {
      mode: Phaser.Scale.FIT,
    },
    scene: [BootScenes, MainScenes],
  };
  const game = new Phaser.Game(config);

  const share = document.querySelector('.share') as HTMLDivElement;

  const img = document.createElement('img');
  img.src = `${ossPath}/images/share.png`;

  share.appendChild(img);

  share.onclick = () => {
    share.style.display = 'none';
  };

  if (!isPro) {
    document.title = `[测试]${document.title}`;
  }

  window.focus();
  resize(game);
  window.addEventListener('resize', () => resize(game), false);
};
